package com.opennews.openplatform.jooq;

import lombok.RequiredArgsConstructor;
import org.jooq.impl.DefaultConfiguration;
import org.springframework.boot.autoconfigure.jooq.DefaultConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@RequiredArgsConstructor
@Configuration
public class JooqConfig {
    /**
     * https://blog.jooq.org/how-to-customise-a-jooq-configuration-that-is-injected-using-spring-boot/
     * https://docs.spring.io/spring-boot/api/java/org/springframework/boot/autoconfigure/jooq/DefaultConfigurationCustomizer.html
     *
     * IMPORTANT!!!
     * DO NOT TRY TO CREATE A NEW CONFIGURATION, SINCE THIS WILL LOSE ALL DEFAULT EXISTING CONFIGURATIONS. For example, transactions, logging, etc.
     * With your new created configuration, you will be surprised to find @Transactional is NOT working.
     * Getting default configuration and adding your custom listener is the best approach.
     */
    @Bean
    public DefaultConfigurationCustomizer configurationCustomiser() {
        return (DefaultConfiguration c) -> c.setRecordListener(new RecordListener(), new UserRecordListener());
    }
}